package verifycode

import (
	"fmt"
	"time"
	"wskyygyso/api/pkg/app"
	"wskyygyso/api/pkg/config"
	"wskyygyso/api/pkg/redis"
)

type RedisStore struct {
	RedisClient *redis.RedisClient
	KeyPrefix   string
}

func (s *RedisStore) Get(key string, clear bool) string {
	keyName := s.KeyPrefix + key
	result := s.RedisClient.Get(keyName)
	if clear {
		s.RedisClient.Del(keyName)
	}
	return result
}

func (s *RedisStore) Set(key string, value string) bool {
	ExpireTime := time.Minute * time.Duration(config.GetInt64("verifycode.expire_time"))
	if app.IsLocal() {
		ExpireTime = time.Minute * time.Duration(config.GetInt64("verifycode.debug_expire_time"))
	}
	keyName := s.KeyPrefix + key
	return s.RedisClient.Set(keyName, value, ExpireTime)
}

func (s *RedisStore) Verify(key string, answer string, clear bool) bool {
	value := s.Get(key, clear)
	fmt.Println("value  " + value)
	return answer == value
}
